package com.emapp.lccode.db

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Transaction
import androidx.room.Update

@Dao
abstract class BaseDao<T> {
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    abstract fun insert(item: T): Long
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    abstract fun insert(items: List<T>?): List<Long>
    @Update
    abstract fun update(item: T)
    @Update
    abstract fun update(items: List<T>?)
    @Transaction
    open fun insertOrUpdate(item: T) {
        val id = insert(item)
        if (id == -1L) update(item)
    }

    @Transaction
    open fun insertOrUpdate(items: List<T>) {
        val result = insert(items)
        val list: MutableList<T> = ArrayList()
        for (i in result.indices) if (result[i] == -1L) list.add(items[i])
        if (list.size > 0) update(list)
    }
}